package com.centit.framework.ip.templateFile.controller;

import com.centit.framework.ip.templateFile.po.VersionFile;
import com.centit.framework.ip.util.layuiResultUtil;
import com.centit.support.algorithm.UuidOpt;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.centit.framework.ip.templateFile.service.VersionFileManager;
	

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray; 
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.core.controller.BaseController;
import com.centit.support.database.utils.PageDesc;

/**
 * VersionFile  Controller.
 * create by scaffold 2018-06-05 
 * @author 脚手架，改成开发者的名字
 * 金格文书版本信息金格文书版本信息   
*/


@Controller
@RequestMapping("/templateFile/versionfile")
public class VersionFileController  extends BaseController {
	private static final Log log = LogFactory.getLog(VersionFileController.class);
	
	@Resource
	private VersionFileManager versionFileMag;	
	

    /**
     * 查询所有   金格文书版本信息  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> searchColumn = convertSearchColumn(request);
        String limit=request.getParameter("limit");//必要写法
        pageDesc.setPageSize(Integer.parseInt(limit));//设置参数
        JSONArray jsonArrayObjects = versionFileMag.listObjectsAsJson( searchColumn, pageDesc);
        if (null == pageDesc) {
            JsonResultUtils.writeSingleDataJson(jsonArrayObjects, response);
            return;
        }
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);
        layuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);
       // JsonResultUtils.writeResponseDataAsJson(resData, response);
    }
    
    /**
     * 查询单个  金格文书版本信息 
	
	 * @param versionid  versionId
     *  
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{versionid}", method = {RequestMethod.GET})
    public void getVersionFile(@PathVariable String versionid, HttpServletResponse response) {
    	
    	VersionFile versionFile =     			
    			versionFileMag.getObjectById( versionid);
        
        JsonResultUtils.writeSingleDataJson(versionFile, response);
    }
    

    /**
     * 删除单个  金格文书版本信息 
	
	 * @param versionid  versionId
     */
    @RequestMapping(value = "/remove/{versionid}", method = {RequestMethod.POST})
    public void deleteVersionFile(@PathVariable String versionid, HttpServletResponse response) {
    	
    	versionFileMag.deleteObjectById( versionid);
        
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    } 
    
    /**
     * 新增或保存 金格文书版本信息 
	 * @param versionFile  {@link VersionFile}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public void updateVersionFile(
    	@Valid VersionFile versionFile, HttpServletResponse response) {
    	if(versionFile!=null&& StringUtils.isNotBlank(versionFile.getVersionid())){
            VersionFile dbVersionFile  =
                    versionFileMag.getObjectById( versionFile.getVersionid());
            dbVersionFile.copyNotNullProperty(versionFile);
            versionFile=dbVersionFile;
        }else {
            versionFile.setVersionid(UuidOpt.getUuidAsString32());
            versionFile.setCreatetime(new Date());
        }
        versionFileMag.mergeObject(versionFile);
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    }
}
